package com.yd.mvc.pc.noticeController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.noticeService.NoticeService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Notice;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.NoticeParam;
import com.yd.domain.sys.DicEnum;
import com.yd.domain.sys.Logger;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zy
 * @create 2018-03-09 14:01
 * @desc 公告的增删改查
 **/
@RestController
@RequestMapping(value = "/api/notice")
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc: 录入公告
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addNotice(@RequestBody Notice n,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.NOTIC_MANAGER);//验证权限
        }
        if(user!=null){
            String suff="id:"+n.getId()+";发布人:"+n.getPublishUser()+";标题:"+n.getTitle()+";状态:"+n.getStatus();
            n.setPublishUser(user.getUserId());//录入人
            try {
                n.setStatus(DicEnum.ENABLE_STATUS.UN_ENABLE);
                noticeService.addNotice(n);
                loggerService.addLogger(new Logger("新增公告记录",user.getUserId(),Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.NOTICE,"",suff),request);
            } catch (Exception e) {
                loggerService.addLogger(new Logger("新增公告记录",user.getUserId(),Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.NOTICE,"",suff),request);
                throw  new BusinessException(500,"添加失败");
            }
        }else{
            throw  new BusinessException(401,"未登录");
        }

    }

    /**
     * 查询
     * @param noticeId
     * @param request
     */
    @RequestMapping(value = "/get/{noticeId}", method = RequestMethod.GET)
    public Notice getNotice(@PathVariable("noticeId")String noticeId,HttpServletRequest request)  {
        Notice notice=null;
        try {
            notice= noticeService.getNoticeById(noticeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return notice;
    }

    /**
     * @Author: zy
     * @Desc: 修改
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateNotice(@RequestBody Notice n,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.NOTIC_MANAGER);//验证权限
            userName=user.getUserName();
        }

        String suff="id:"+n.getId()+";发布人:"+n.getPublishUser()+";标题:"+n.getTitle()+";状态:"+n.getStatus();
        try {
            noticeService.updateNotice(n);
            loggerService.addLogger(new Logger("修改公告记录",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.NOTICE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改公告记录",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.NOTICE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 修改
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/enable/{noticeId}", method = RequestMethod.GET)
    public void enableNotice(@PathVariable("noticeId")String noticeId,  HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String suff="";
        try {
            noticeService.enableNotice(noticeId);
        } catch (Exception e) {
            throw  new BusinessException(500,"公告启用失败");
        }
    }
    /**
     * @Author: zy
     * @Desc: 删除
     * @Date: 2018/3/9
     * @Param
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteNotice(@PathVariable("id") String id,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.NOTIC_MANAGER);//验证权限
            userName=user.getUserName();
        }
        String suff="id:"+id;
        try {
            noticeService.deleteNotice(id);
            loggerService.addLogger(new Logger("删除公告记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.NOTICE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("删除公告记录",userName,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.NOTICE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 审核
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    public void verifyNotice(@RequestBody Notice notice,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.NOTIC_MANAGER);//验证权限
            userName=user.getUserName();
        }
        String suff="id:"+notice.getId()+";状态:"+notice.getStatus();
        try {
            noticeService.verifyNotice(notice);
            loggerService.addLogger(new Logger("审核公告记录",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.NOTICE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("审核公告记录",userName,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.NOTICE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 分页查询数据列表
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getNoticeList(@RequestBody NoticeParam noticeParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=noticeService.getNoticeList(noticeParam,pageIndex,pageSize);
        return pm;
    }
}
